package com.hzw.saas.service.product.service;

import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.product.IPaymentOnlineService;
import com.hzw.saas.api.product.dto.PaymentOnlineDto;
import com.hzw.saas.common.config.exception.DefaultSaasException;
import com.hzw.saas.common.util.IdWorker;
import com.hzw.saas.service.product.mapper.PaymentOnlineMapper;
import com.hzw.saas.service.product.model.PaymentOnline;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 * 支付表 服务实现类
 * </p>
 *
 * @author zzl
 * @since 2021-01-21
 */
@Service("paymentOnlineServiceImpl")
@RequiredArgsConstructor
public class PaymentOnlineServiceImpl extends ServiceImpl<PaymentOnlineMapper, PaymentOnline> implements IPaymentOnlineService, IService<PaymentOnline> {

    private final MapperFacade mapperFacade;
    private final IdWorker idWorker;

    @Override
    public PaymentOnlineDto get(String payId) {
        if (StringUtils.isBlank(payId)) {
            return null;
        }
        PaymentOnline paymentOnline = this.getById(payId);
        return mapperFacade.map(paymentOnline, PaymentOnlineDto.class);
    }

    @Override
    public PaymentOnlineDto getByPayNo(String payNo) {
        if (StringUtils.isBlank(payNo)) {
            return null;
        }
        PaymentOnline paymentOnline = this.lambdaQuery().eq(PaymentOnline::getPayNo, payNo).one();
        return mapperFacade.map(paymentOnline, PaymentOnlineDto.class);
    }

    @Override
    public PaymentOnlineDto getByOrder(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return null;
        }
        PaymentOnline paymentOnline = this.lambdaQuery().eq(PaymentOnline::getOrderId, orderId).one();
        return mapperFacade.map(paymentOnline, PaymentOnlineDto.class);
    }

    @Override
    public PaymentOnlineDto add(PaymentOnlineDto paymentOnlineDto) {
        if (StringUtils.isBlank(paymentOnlineDto.getPayId())) {
            paymentOnlineDto.setPayId(String.valueOf(idWorker.nextId()));
        }
        if (paymentOnlineDto.getCreateTime() == null) {
            paymentOnlineDto.setCreateTime(new Date());
        }
        PaymentOnline paymentOnline = mapperFacade.map(paymentOnlineDto, PaymentOnline.class);
        this.save(paymentOnline);
        return paymentOnlineDto;
    }

    @Override
    public boolean update(PaymentOnlineDto paymentOnlineDto) {
        if (StringUtils.isBlank(paymentOnlineDto.getPayId())) {
            throw DefaultSaasException.build("支付ID为空！").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOnline paymentOnline = mapperFacade.map(paymentOnlineDto, PaymentOnline.class);
        return this.updateById(paymentOnline);
    }

    @Override
    public PaymentOnlineDto delete(String payId) {
        if (StringUtils.isBlank(payId)) {
            throw DefaultSaasException.build("支付ID为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOnlineDto paymentOnlineDto = this.get(payId);
        boolean remove = this.removeById(payId);
        if (!remove) {
            throw DefaultSaasException.build("删除失败");
        }
        return paymentOnlineDto;
    }

    @Override
    public PaymentOnlineDto deleteByPayNo(String payNo) {
        if (StringUtils.isBlank(payNo)) {
            throw DefaultSaasException.build("支付号为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOnlineDto paymentOnlineDto = this.getByPayNo(payNo);
        boolean remove = this.remove(Wrappers.<PaymentOnline>lambdaQuery().eq(PaymentOnline::getPayNo, payNo));
        if (!remove) {
            throw DefaultSaasException.build("删除失败");
        }
        return paymentOnlineDto;
    }

    @Override
    public PaymentOnlineDto deleteByOrder(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw DefaultSaasException.build("订单号为空").status(HttpStatus.BAD_REQUEST);
        }
        PaymentOnlineDto paymentOnlineDto = this.getByOrder(orderId);
        boolean remove = this.remove(Wrappers.<PaymentOnline>lambdaQuery().eq(PaymentOnline::getOrderId, orderId));
        if (!remove) {
            throw DefaultSaasException.build("删除失败");
        }
        return paymentOnlineDto;
    }

}
